(function() {
	var value = 0;
	var t;
	var running = false;
	var spent = 0;

	$('fieldset[name="seller-cids"] legend input:checkbox').change(function(){
		var checked = $(this).attr('checked');
		var fieldSet = $(this).closest('fieldset');
		if (checked)
		{
			fieldSet.removeClass("collapse");
		}
		else
		{
			fieldSet.addClass('collapse');
		}
	});
	
	$('div.category-item input:checkbox').change(function(){
		var cid = $(this).parent().attr('value');
		var pid = $(this).parent().attr('parent');
		if (parseInt(pid) == 0)
		{
			var v = $(this).attr('checked');
			if (v)
			{
				$('div[parent="' + cid + '"] input').attr('checked', 'checked');
			}
			else
			{
				$('div[parent="' + cid + '"] input').removeAttr('checked');
			}
		}
	});
	
	function getParams()
	{
		var scope = [];
		$(".init .scope input:checked").each(function() {
			scope.push($(this).val());
		});
		var cids = null;
		if ($('fieldset[name="seller-cids"] legend input:checkbox').attr('checked'))
		{
			var checkboxes = $("fieldset[name='seller-cids'] input:checked");
			if (checkboxes)
			{
				cids = [];
				checkboxes.each(function(){
					cids.push($(this).parent().attr('value'));
				});
			}
		}
		var params = {
			'scope.saleStatus': scope.join(),
			'scope.sellerCids': (cids == null ? null : cids.join())
		};
		return params;
	}
	
	function updateStatus()
	{
		if (!running)
		{
			clearInterval(t);
			return;
		}
		$.ajax({
			url: 'status.action',
			success: function( data ) {
				if (running)
				{
					var $data = $(data);
					if (running && $data.is('div'))
					{
						var total = parseInt($('.total', $data).text());
						var progress = parseInt($('.progress', $data).text());
						var oldValue = value;
						value = progress * 100 / total; 
						//alert("total: " + total + " progress: " + progress + " value: " + value);
						if (value > oldValue)
						{
							$('#msg').html(data);
							$("#progressbar").progressbar({
								value: value
							});
						}
					}
					else if ($data.is("table"))
					{
						$('#result').html(data);
						running = false;
						var table = $('#result').find("table");
						var total = parseInt(table.attr("total"));
						var spend = table.attr("spend");
						var spendTxt = spend + " 秒。";
						if (spend > 60)
						{
							var m = parseInt(spend / 60);
							var s = spend % 60;
							spendTxt = m + " 分 " + s + " 秒。";
						}
						$('#msg').html("扫描完成，检查出问题宝贝 <span class='problem'>" + total + "</span> 件，耗时 <span class='spend'>" + spendTxt + "</span>");
						$("#progressbar").progressbar("destroy");
						$(".redetect-button").parent().show();
						if (total == 0)
						{
							//$('#result').html('<div class="congratulation">恭喜您！未检出问题宝贝。</div>');
						}
						spent = 0;
						$('.detect-button').button('enable');
					}
				}
			}
		});
		spent = spent + 2;
		$('#spent span').text(spent);
	};
	
	function detect(params, callback)
	{
		$('#result').empty();
		$.ajax({
			url: 'detect.action',
			data: params,
			success: function( data ) {
				callback();
			},
			error: function(jqXHR, textStatus, errorThrown) {
				alert(jqXHR.responseText);
			}
		});
	};

	$("button").button();

	$(".detect-button").click(function(){
		$(this).button('disable');
		$.ajax({
			url: 'check_interval.action',
			success: function( data ) {
				var limit = $(data).attr("limit");
				if (limit == 'true')
				{
					$("#dialog").html(data).dialog();
					$('.detect-button').button('enable');
					return false;
				}
				var params = getParams();
				$(".init").hide();
				$("#toolbar").show();
				detect(params, function(){
					running = true;
					t = setInterval(updateStatus, 2000);
				});
				$('#tab-detect').data('options', params);
			}
		});
	});

	$(".redetect-button").click(function(){
		$( "#tabs" ).tabs('load', 0);
	});
	
})();
